/**
 * ship-device import
 */
let _this;
let _loading;
new Vue({
    el: '#appContainVue',
    data: {
        excelUrl: '',
        size: 0,
        paramsData: {
            token: '',
            shipId: '',
            shipName: '',
            implementTime: ''
        },
        file: '',
        fileList: [],
        uploadUrl: "/admin/v1/paymentBill/uploadExcel"
    },
    mounted() {
        _this = this;
        let u = localStorage.getItem("ADMIN_LOGIN");
        let loginU = decryptLocalStorage(u);
        _this.paramsData.token = loginU.token;
        // if (loginU.userType != '1') {
        //     _this.paramsData.shipId=loginU.shipId;
        //     _this.paramsData.shipName=loginU.shipName;
        // }
    },
    methods:
        {
            handleRemove(file, fileList) {
                // console.log(file, fileList);
                // this.fileList=[];
            },
            handleChange(file, fileList) {
                // console.log(file);
                this.file = file;
                // this.fileList.push(file);
            },
            handlePreview(file) {
                // console.log(file);
            },
            handleAvatarSuccess(res, file) {
                console.log(res.data);
                _this.file = '';
                _this.fileList = [];
                this.excelUrl = URL.createObjectURL(file.raw);
                if (_loading != undefined) {
                    layer.close(_loading);
                }
                if (res.data != undefined) {
                    if (res.data[0] == 1) {
                        layer.msg('导入成功!', {
                            icon: 1,
                            time: 500
                        }, function () {
                            // 关闭弹出层
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    } else {
                        layer.msg(res.data[1], {
                            icon: 2,
                            time: 5000
                        });
                    }

                } else {
                    layer.msg('导入失败!', {
                        icon: 2,
                        time: 5000
                    });
                }

            },
            beforeAvatarUpload(file) {
                // const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                // this.size=fileLengthFormat(file.size,1);
                // if(!(isExcel)){
                //     this.$message.error('上传只能是xls,xlsx格式!');
                //     return false;
                // }
            },
            submitUpload() {
                console.log(_this.file);
                if (_this.file == '' || _this.file == undefined) {
                    this.$message.error('请选择文件!');
                    return false;
                }
                if (_this.paramsData.shipId == '' || _this.paramsData.shipName == '') {
                    this.$message.error('请选择船舶!');
                    return false;
                }
                _loading = layer.load(2, {shade: [0.5, '#fff']});
                this.$refs.upload.submit();
            }
        }
});
layui.use(['form', 'tableSelect', 'layedit', 'laydate'], function () {
    var form = layui.form,
        layer = layui.layer,
        tableSelect = layui.tableSelect,
        layedit = layui.layedit,
        laydate = layui.laydate,
        $ = layui.$;


    laydate.render({
        elem: '#implementTime'
        , done: function (value, date, endDate) {
            _this.paramsData.implementTime = value;
        }
    });

    let u = localStorage.getItem("ADMIN_LOGIN");
    let loginU = {};
    if (u == undefined) {
        layer.msg('请登录', {icon: 1, time: 500}, function () {
            window.location = 'page/login.html';
        });
    } else {
        loginU = decryptLocalStorage(u);
    }
    if (loginU.shipId != 0) {
        _this.paramsData.shipId = loginU.shipId;
    }
    if (loginU.userType != '1') {
        // $("#shipId").val(loginU.shipId);
        // $("#shipName").val(loginU.shipName);
        // $("#shipName").attr("disabled", "disabled");
    }
    loadShip();

    //ship
    function loadShip() {
        let shipTableUrl = "/admin/v1/ship/page?shipStatus=营运&token=" + loginU.token;
        tableSelect.render({
            elem: '#shipName',
            searchFlag: true,
            searchKey: 'shipName',
            checkedKey: 'id',
            table: {
                url: shipTableUrl,
                cols: [[
                    {type: 'radio'},
                    {field: 'shipId', title: 'ID'},
                    {field: 'shipCompanyName', title: '船舶公司'},
                    {field: 'shipName', title: '船舶'}
                ]]
            },
            done: function (elem, data) {
                if (data.data.length > 0) {
                    _this.paramsData.shipId = data.data[0].shipId;
                    _this.paramsData.shipName = data.data[0].shipName;
                }
            },
            clear: function () {
                _this.paramsData.shipId = '';
                _this.paramsData.shipName = '';
            }
        })
    }
});

